package fr.cie.blog.modeldao.hib;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import fr.cie.blog.beans.Card;
import fr.cie.blog.beans.Style;
import fr.cie.blog.domainservice.IServiceStyle;

/**
 * @author AetM
 * 
 */
public class StyleDaoImplHib extends HibernateDaoSupport implements
		IServiceStyle {

	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger.getLogger(StyleDaoImplHib.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#save(java.lang.Object)
	 */
	@Override
	public final int save(final Style style) {
		// WARN: getHibernateTemplate doesn't work !
		// getHibernateTemplate().save(style);

		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(style);
			transaction.commit();

		} catch (DataAccessException e) {
			transaction.rollback();
			LOG.error("save Style error", e);

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("save Style error", e);
		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#update(java.lang.Object)
	 */
	@Override
	public final int update(final Style style) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(style);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Style error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#delete(java.lang.Object)
	 */
	@Override
	public final int delete(final Style style) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.delete(style);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("delete Style error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findAll()
	 */
	@Override
	public final List<Style> findAll() {
		@SuppressWarnings("unchecked")
		final List<Style> list = (List<Style>) getHibernateTemplate().find(
				"from Style");
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.cie.blog.domainservice.IServiceStyle#findByCard(fr.cie.blog.beans.
	 * Card)
	 */
	@Override
	public final Style findByCard(final Card pCard) {
		final Style style = (Style) getHibernateTemplate().findByNamedParam(
				"from Style as s where s.card.id = :param ", "param",
				pCard.getId()).get(0);
		return style;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findById(int)
	 */
	@Override
	public final Style findById(final long pIdentifier) {
		return getHibernateTemplate().get(Style.class, pIdentifier);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceStyle#findDefaultStyle()
	 */
	@Override
	public final Style findDefaultStyle() {
		final Style style = (Style) getHibernateTemplate().findByNamedParam(
				"from Style as s where s.status = :param ", "param",
				"default").get(0);
		return style;
	}

	@Override
	public final Style findByName(final String pName) {
		final Style style = (Style) getHibernateTemplate().findByNamedParam(
				"from Style as s where s.name = :param ", "param", pName)
				.get(0);
		return style;
	}
}